/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUISnakesAndLadders.java
 *
 * Created on 7/12/2010, 12:03:27 AM
 */

package snakesandladders;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author andres
 */



public class GUISnakesAndLadders extends javax.swing.JFrame implements ActionListener{

    /** Creates new form GUISnakesAndLadders */

    Casilla matrizCasillas[];
    int tamanoTablero,tocaTurno;
    AdminJuego admin;
    Timer tiempo;
    Timer tiempoTurno,animacion;
    int casAJ,casAM,casNJ,casNM;

    public GUISnakesAndLadders(int tamanoTablero) {
        this.tamanoTablero=tamanoTablero;
        initComponents();
        matrizCasillas=new Casilla[tamanoTablero*tamanoTablero];
        admin=new AdminJuego(matrizCasillas);
        tocaTurno=1;
        tiempo=new Timer(3000, this);
        animacion=new Timer(300, this);
        tiempoTurno=new Timer(2000, this);
        crearLabels();

    }


    public void pintarJugada()
    {
        int posJug,posMaq;
        posJug=admin.casillaActualJug();
        posMaq=admin.casillaActualMaq();

        JLabel label1=matrizCasillas[posJug].getLabel();
        label1.setBackground(Color.yellow);

        JLabel label2=matrizCasillas[posMaq].getLabel();
        label2.setBackground(Color.red);
    }


    public void juegaMaquina()
    {
        admin.realizarJugada(2);
        tocaTurno=1;
    }

    public void pintarCulebras()
    {
        matrizCasillas[26].setColaCulebra(9);
        matrizCasillas[30].setColaCulebra(3);
        matrizCasillas[34].setColaCulebra(28);
        matrizCasillas[54].setColaCulebra(43);
        matrizCasillas[60].setColaCulebra(52);
        matrizCasillas[62].setColaCulebra(47);

        (matrizCasillas[26].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[30].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[34].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[54].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[60].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[62].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[9].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[3].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[28].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[43].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[52].getLabel()).setBackground(Color.yellow);
        (matrizCasillas[47].getLabel()).setBackground(Color.yellow);
    }

    public void pintarEscaleras()
    {

        (matrizCasillas[11].getLabel()).setIcon(new ImageIcon("src/imagenes/ladderG1.jpeg"));
        (matrizCasillas[27].getLabel()).setIcon(new ImageIcon("src/imagenes/ladderG2.jpeg"));
        (matrizCasillas[16].getLabel()).setIcon(new ImageIcon("src/imagenes/ladderR1.jpeg"));
        (matrizCasillas[32].getLabel()).setIcon(new ImageIcon("src/imagenes/ladderR2.jpeg"));
        (matrizCasillas[40].getLabel()).setIcon(new ImageIcon("src/imagenes/ladderW1.jpeg"));
        (matrizCasillas[56].getLabel()).setIcon(new ImageIcon("src/imagenes/ladderW2.jpeg"));

        matrizCasillas[11].setFinEscalera(27);
        matrizCasillas[16].setFinEscalera(32);
        matrizCasillas[40].setFinEscalera(56);
    }

    public void crearLabels()
    {
        int contador=0;
        int x,y;
        y=x=501;
        for(int i=tamanoTablero-1; i>=0 ; i--)
        {
            y=(63*i);
            if((i%2)!=0)
            {
                for(int j=0; j<tamanoTablero; j++)
                {
                    x=(63*j);
                    JLabel label=new JLabel((contador+1)+"");
                    label.setBounds(x, y, 60, 60);
                    label.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
                    jLayeredPane1.add(label, javax.swing.JLayeredPane.DEFAULT_LAYER);
                    matrizCasillas[contador]=new Casilla(label,contador);
                    contador++;

                }
            }
            else
            {
                 for(int j=tamanoTablero-1; j>=0; j--)
                {
                    x=(63*j);
                    JLabel label=new JLabel((contador+1)+"");
                    label.setBounds(x, y, 60, 60);
                    label.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
                    jLayeredPane1.add(label, javax.swing.JLayeredPane.DEFAULT_LAYER);
                    matrizCasillas[contador]=new Casilla(label,contador);
                    contador++;

                }
            }
            
        }

        pintarCulebras();
        pintarEscaleras();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jlDado = new javax.swing.JLabel();
        jbTirarDado = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Snakes And Ladders");

        jButton2.setText("Reiniciar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jlDado.setFont(new java.awt.Font("DejaVu Sans", 0, 36));
        jlDado.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlDado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Precipicio/3.jpg"))); // NOI18N
        jlDado.setBorder(null);

        jbTirarDado.setText("Tirar Dado");
        jbTirarDado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbTirarDadoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 619, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(17, 17, 17)
                                        .addComponent(jbTirarDado, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(53, 53, 53)
                                        .addComponent(jlDado, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addComponent(jButton2))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(339, 339, 339)
                        .addComponent(jLabel1)))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(jlDado, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbTirarDado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 247, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(126, 126, 126))
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 553, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbTirarDadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbTirarDadoActionPerformed
        jlDado.setIcon(new ImageIcon("dado.gif"));
        tiempo.start();
    }//GEN-LAST:event_jbTirarDadoActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
       tocaTurno=1;
       admin=new AdminJuego(matrizCasillas);

    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUISnakesAndLadders(8).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JButton jbTirarDado;
    private javax.swing.JLabel jlDado;
    // End of variables declaration//GEN-END:variables
int numCasilla;
    private void jugar() {
        // TODO add your handling code here:
         if(tocaTurno==1)
        {
             jbTirarDado.setEnabled(false);
             numCasilla=admin.casillaActualJug();
             casAJ=numCasilla;
             
             numCasilla=admin.casillaActualMaq();
             casAM=numCasilla;
             
            admin.realizarJugada(1);
             
             Thread hilo=new Thread() {
                public  void run()
                        {
                 
                            while(!admin.asignoCasilla)
                            {
                                System.out.println("");
                            }
                            numCasilla=admin.casillaActualJug();
                            casNJ=numCasilla;
                            
                            numCasilla=admin.casillaActualMaq();
                            casNM=numCasilla;
                            
                            
                            tocaTurno = 2;
                            animacion.start();
                            stop();
                        }
            };
            hilo.start();
            
        }
        else        
        {
        //     JOptionPane.showMessageDialog(null,  "Todavia no es tu turno","Error", JOptionPane.ERROR_MESSAGE);
              numCasilla=admin.casillaActualMaq();
             casAM=numCasilla;
             numCasilla=admin.casillaActualJug();
             casAJ=numCasilla;
             juegaMaquina();
             Thread hilo=new Thread() {
                public  void run()
                        {
                         
                            while(!admin.asignoCasilla)
                            {
                                       System.out.println("");
                            }

                            numCasilla=admin.casillaActualJug();
                            casNJ=numCasilla;
                            numCasilla=admin.casillaActualMaq();
                            casNM=numCasilla;
                            tocaTurno = 1;
                            animacion.start();
                            stop();
                            }
             };hilo.start();
        }
    }

    public void actionPerformed(ActionEvent ae) {
        Object o=ae.getSource();

        if(o==tiempo)
        {
            tiempo.stop();
            jlDado.setIcon(new ImageIcon(admin.dado()+".jpg"));
            jugar();
        }

        if(o==tiempoTurno)
        {
            tiempoTurno.stop();
            jlDado.setIcon(new ImageIcon("dado.gif"));
            tiempo.start();
        }
        if(o==animacion)
        {
            if(casAJ==casNJ&&casAM==casNM)
            {
                animacion.stop();
                if(tocaTurno==1)
                {
                    if(!admin.verificarFinJuego())
                    {
                        jbTirarDado.setEnabled(true);
                    }
                }
                if(tocaTurno==2)
                {
                    if(!admin.verificarFinJuego())
                    {

                        tiempoTurno.start();
                    }
                }

            }
            if(casAJ<casNJ)
            {
                matrizCasillas[casAJ].getLabel().setBackground(Color.white);
                casAJ++;
                
            }
            if(casAJ>casNJ)
            {
                matrizCasillas[casAJ].getLabel().setBackground(Color.white);
                casAJ--;
                
            }
            if(casAM<casNM)
            {
                matrizCasillas[casAM].getLabel().setBackground(Color.white);
                casAM++;
            }
            if(casAM>casNM)
            {
                matrizCasillas[casAM].getLabel().setBackground(Color.white);
                casAM--;
                
            }
            matrizCasillas[casAJ].getLabel().setBackground(Color.blue);
            matrizCasillas[casAM].getLabel().setBackground(Color.green);
        }
    }

}
